/*
 * ItemResource.h
 *
 *  Created on: 2013. 9. 11.
 *      Author: jjaehuny
 */

#ifndef PROJECTZ_NRESOURCE_ITEMRESOURCE_H_
#define PROJECTZ_NRESOURCE_ITEMRESOURCE_H_

#include "../NLogic/Item.h"
#include "../NLogic/Card.h"
#include "BalanceResource.h"

namespace ProjectZ { namespace NResource {

class ItemResource
{
public:
	struct Key
	{
		Key() : _tid(0), _class_type(0) {}
		Key(int tid, int class_type) : _tid(tid), _class_type(class_type) {}

		int _tid;
		int _class_type;

		bool operator == (const Key & rhs) const
		{
			if (this->_tid == rhs._class_type && this->_tid == rhs._class_type)
				return true;

			return false;
		}

		bool operator < (const Key & rhs) const
		{
			if (this->_tid < rhs._tid)
			{
				return true;
			}
			else if (this->_tid == rhs._tid)
			{
				if (this->_class_type < rhs._class_type)
					return true;
			}

			return false;
		}
	};

public:
	struct Flyweight
	{
		int		_TID;
		int 	_CLASSITEMTABLETYPE;	// string
		int		_ICON;
		Toolkit::Str<100>	_NAME_KOR;
		Toolkit::Str<100>	_NAME_ENG;
		Toolkit::Str<100>	_NAME_JPN;
//	  `DLC_TYPE` int(11) NOT NULL,	// string
		int		_USING;				// string
		int		_DESTROY;				// string
		int		_SALE;					// string
		int		_ITEM_EXCHANGE;		// string
		int		_STACK;				// string
		int		_BAGTYPE;				// string
		int		_SUBTYPE;				// string
		int		_CHARCLASSTYPE;		// string
		int		_PRICE;
		int		_DURABILITY;
		int		_LIMIT_LEVEL;
		int		_EFFECT;				// string
		int		_EFF_VALUE;
		int		_EFF_POS;
//		`EFF_POS` int(11) NOT NULL	// string
	};

public:
	typedef std::map<Key, Flyweight *> 		ITEMRESOURCETREE;
	typedef std::vector<ITEMRESOURCETREE> 		ITEMRESOURCETREEVECTOR;

public:
	ItemResource();
	virtual 		~ItemResource();

public:
	bool 			LoadResource();

	bool 			SetItemDataFromResource(User * pUser, NLogic::Item::Data & clsData);
	bool 			SetItemFromXLS(User * pUser, NLogic::Item::Data & clsData, int sheet, int tid, int quantity);

	const char *	GetItemName(NLogic::Item::Data & clsData);
	int 			GetIconIndex(NLogic::Item::Data & clsData);
	int 			GetItemBagType(NLogic::Item::Data & clsData);
	int 			GetItemSubType(NLogic::Item::Data & clsData);
	int 			GetItemPrice(NLogic::Item::Data & clsData);
	int 			GetItemDurability(NLogic::Item::Data & clsData);
	int 			GetItemLevel(NLogic::Item::Data & clsData);
	int 			GetItemOpenSlotCount(NLogic::Item::Data & clsData);
	int 			GetItemMaxRefine(NResource::BalanceData * pBalance, NLogic::Item::Data & clsData);
	void 			setItemEffect(NLogic::Item::Data & clsData);
	void			setItemEffectForCardReward(NLogic::Card::Data * pData);
	void 			SetAdditionalEffect(NLogic::Item::Data & clsData, bool bFairySacrifice=false);

	int				getItemTradeType(const NLogic::Item::Data & clsData);
	int				getItemStackType(const NLogic::Item::Data & clsData);
	int				getItemSaleType(const NLogic::Item::Data & clsData);
	bool			isUpstone(const NLogic::Item::Data & clsData);
	bool 			isEquip(const NLogic::Item::Data & clsData);
	bool 			isAccessary(const NLogic::Item::Data & clsData);
	bool			isFairy(const NLogic::Item::Data & clsData);
	bool			isBattlePet(const NLogic::Item::Data & clsData);


	ITEMRESOURCETREE &	GetItemResourceTree(EnumClassItemTableType eClassItemTableType);

private:
	int 			GetItemUsing(NLogic::Item::Data & clsData);

	int 			GetClassType(User *pUser, int tid, int sheet_type);
	bool 			IsItemIdentify(NResource::BalanceData * pBalance, NLogic::Item::Data & clsData);
	int 			GetItemSettedType(NResource::BalanceData * pBalance, NLogic::Item::Data & clsData);
	int 			GetItemBelong(NResource::BalanceData * pBalance, NLogic::Item::Data & clsData);

private:
	int 			getItemRoutingValue(NLogic::Item::Data & clsData, int slotIndex);
	int 			getAdditionalEffectType(NLogic::Item::Data & clsData);
	int 			getAdditionalEffectValue(NLogic::Item::Data & clsData, int effectType, int upAddPercent, int slotIndex);
	int 			getAdditionalEffectPos(int effectType);

	Flyweight *	getFly(int sub_type, int tid, int class_type);

private:
	int 			getRandBetween(int minVal, int maxVal);
	void			Clear();

private:
	ITEMRESOURCETREEVECTOR _clsItemResourceTreeVector;

private:
	std::string emptyString;

};

} /*NResource*/ } /* namespace ProjectZ */

#endif /* PROJECTZ_NRESOURCE_ITEMRESOURCE_H_ */
